package com.zhkj.txg.module.find.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import com.zhkj.lib.utils.ToastUtil
import com.zhkj.lib.widget.ClearEditText
import com.zhkj.txg.R
import razerdp.basepopup.BasePopupWindow
import razerdp.util.KeyboardUtils

class FindCommentInputPopupWindow(
    context: Context,
    val commit: (content: String) -> Unit,
    val doOnDismiss: () -> Unit
) : BasePopupWindow(context) {

    private var editText: ClearEditText? = null

    init {
        popupGravity = Gravity.BOTTOM
        setAdjustInputMethod(true)
        setOutSideDismiss(true)
        setBackgroundColor(0x11000000)
        setAdjustInputMode(FLAG_KEYBOARD_ALIGN_TO_ROOT or FLAG_KEYBOARD_FORCE_ADJUST)
        isOutSideTouchable = false
        onDismissListener = object : OnDismissListener() {
            override fun onDismiss() {
                doOnDismiss()
            }
        }
    }

    override fun onCreateContentView(): View = createPopupById(R.layout.popup_find_input)

    override fun onViewCreated(contentView: View?) {
        editText = contentView?.findViewById(R.id.tvFindArticleComment)
        contentView?.findViewById<View>(R.id.ivFindCommit)?.setOnClickListener {
            if (editText?.text.isNullOrEmpty()) {
                ToastUtil.show(context.resources.getString(R.string.empty_content))
                return@setOnClickListener
            }
            commit(editText?.text?.toString() ?: "")
        }
    }

    override fun showPopupWindow() {
        super.showPopupWindow()
        KeyboardUtils.open(context)
    }

    override fun dismiss() {
        KeyboardUtils.close(editText)
        editText?.postDelayed({
            super.dismiss()
        }, 200)
    }

    fun setContent(content: String) {
        editText?.setText(content)
        editText?.setSelection(content.length)
    }

    fun getContent(): String {
        return editText?.text?.toString() ?: ""
    }
}